package com.gdones.bootserver.bus.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdones.bootserver.bus.role.dto.Role;
import com.gdones.bootserver.bus.role.dto.RoleMenu;
import com.gdones.bootserver.bus.role.dto.RoleMenuUpdateDto;
import com.gdones.bootserver.bus.role.mapper.RoleMapper;
import com.gdones.bootserver.bus.role.mapper.RoleMenuMapper;
import com.gdones.bootserver.bus.role.service.IRoleService;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-03-28
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public MyResult getPage(Role role, MyPageBand pager) {
        MyResult result = new MyResult();

        RoleMapper baseMapper = getBaseMapper();

        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();

        if(role.getName()!=null){
            queryWrapper.or()
                    .like("code",role.getName())
                    .or().like("name",role.getName());
        }

        Page<Role> pageDto = new Page<Role>(pager.getPage(), pager.getSize());
        Page<Role> rolePage = baseMapper.selectPage(pageDto, queryWrapper);

        Map<String,Object> resMap = new HashMap<>();
        resMap.put("total",rolePage.getTotal());
        resMap.put("pageData",rolePage.getRecords());
        resMap.put("page",pager.getPage());
        resMap.put("limit",pager.getSize());

        result.setMsg("查询成功！");
        result.setData(resMap);

        return result;
    }

    @Override
    public MyResult deletes(String ids) {
        MyResult result = new MyResult();

        String[] split = ids.split(",");

        List<String> roleIDList = Arrays.asList(split);

        RoleMapper baseMapper = getBaseMapper();

        UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();

        updateWrapper.in("roleID",roleIDList);

        int delete = baseMapper.delete(updateWrapper);

        if(delete>0){
            result.setMsg("删除成功！");
        }else{
            result.setCode("9999");
            result.setMsg("删除失败！");
        }

        return result;
    }

    @Override
    public MyResult updateRole(Role role) {
        MyResult result = new MyResult();

        RoleMapper baseMapper = getBaseMapper();

        UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();

        updateWrapper.eq("roleID",role.getRoleID());

        int update = baseMapper.update(role, updateWrapper);

        if(update>0){
            result.setMsg("更新成功！");
        }else{
            result.setCode("9999");
            result.setMsg("更新失败！");
        }

        return result;
    }

    @Override
    public MyResult addRole(Role role) {
        MyResult result = new MyResult();

        RoleMapper baseMapper = getBaseMapper();

        //先查询编号是否有重复
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("code",role.getCode());

        List<Role> roles = baseMapper.selectList(queryWrapper);

        if(roles.size()>0){
            result.setMsg("编号["+role.getCode()+"]已存在，请换一个编号！");
            result.setCode("9999");
            return result;
        }

        int insert = baseMapper.insert(role);

        if(insert>0){
            result.setMsg("添加成功！");
        }else{
            result.setCode("9999");
            result.setMsg("添加失败！");
        }

        return result;
    }

    @Override
    public MyResult getRoleByID(String roleID) {
        MyResult result = new MyResult();

        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("roleID",roleID);

        RoleMapper baseMapper = getBaseMapper();

        Role role = baseMapper.selectOne(queryWrapper);

        if(role!=null){
            result.setMsg("查询成功！");
            result.setData(role);
        }else{
            result.setCode("9999");
            result.setMsg("查询失败！");
        }

        return result;
    }

    @Override
    public MyResult updateMenuRoles(RoleMenuUpdateDto roleMenuUpdateDto) {
        MyResult result = new MyResult();

        //先把旧数据删除
        roleMenuMapper.deleteDataForRoleID(roleMenuUpdateDto.getRoleID());

        List<String> menuIDs = roleMenuUpdateDto.getMenuIDs();

        boolean isOk = false;

        for(String menuID:menuIDs){

            RoleMenu saveDO = new RoleMenu();

            saveDO.setMenuID(menuID);
            saveDO.setRoleID(roleMenuUpdateDto.getRoleID());

            int insert = roleMenuMapper.insert(saveDO);

            if(insert>0){
                isOk = true;
            }else{
                isOk = false;
                break;
            }

        }

        if(isOk){
            result.setMsg("菜单权限更新成功！");
        }else{
            result.setMsg("菜单权限更新出错！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult getRoleMenuListByRoleID(String roleID) {
        MyResult result = new MyResult();

        QueryWrapper<RoleMenu> roleMenuQueryWrapper = new QueryWrapper<RoleMenu>();

        roleMenuQueryWrapper.eq("roleID",roleID);

        List<RoleMenu> roleMenus = roleMenuMapper.selectList(roleMenuQueryWrapper);

        result.setMsg("菜单权限数据查询成功！");
        result.setData(roleMenus);

        return result;
    }
}
